package com.thinkgem.jeesite.kpoint.core.mybatisplus;


import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.injector.methods.*;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;

import java.util.List;

/**
 * @Author:Kongxiangsheng
 * @DesCription:
 * @Date:Created in 21:40 2018/12/15
 * @Modified By:
 */
public class KpointLogicSqlInjector extends LogicSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList() {
        List<AbstractMethod> list = super.getMethodList();
        list.add(new LogicSelectByEntity());
        return list;
    }

//    @Override
//    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
//        selectByEntity(mapperClass, modelClass, table);
//    }
//
//    public void selectByEntity(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
//        SqlMethod sqlMethod = SqlMethod.SELECT_LIST;
//        /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
//        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(table, false), table.getTableName(), sqlWhereByEntity(table));
//
//        /* mapper 接口方法名一致 */
//        String method = "selectByEntity";
//        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
//
//        // 注意！！ 这里是更新、删除、插入、调用方法注入不一样！！
//        this.addSelectMappedStatement(mapperClass, method, sqlSource, modelClass, table);
//    }
//
//    protected String sqlWhereByEntity(TableInfo table) {
//        if (table.isLogicDelete()) {
//            StringBuilder where = new StringBuilder("\n<where>");
//            // 过滤逻辑
//            List<TableFieldInfo> fieldList = table.getFieldList();
//            // EW 逻辑
//            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
//                where.append("\n<if test=\"").append(table.getKeyProperty()).append("!=null\">");
//                where.append(" AND ").append(table.getKeyColumn()).append("=#{");
//                where.append(table.getKeyProperty()).append("}");
//                where.append("</if>");
//            }
//            for (TableFieldInfo fieldInfo : fieldList) {
//                where.append(convertIfTag(fieldInfo, "", false));
//                where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(), fieldInfo.getColumn(), "" + fieldInfo.getEl()));
//                where.append(convertIfTag(fieldInfo, true));
//            }
//            // 过滤逻辑
//            where.append("\n").append(getLogicDeleteSql(table));
//            where.append("\n</where>");
//            return where.toString();
//        } else {
//            StringBuilder where = new StringBuilder();
//            where.append("\n<where>");
//            if (StringUtils.isNotEmpty(table.getKeyProperty())) {
//                where.append("\n<if test=\"").append(table.getKeyProperty()).append("!=null\">\n");
//                where.append(table.getKeyColumn()).append("=#{").append(table.getKeyProperty()).append("}");
//                where.append("\n</if>");
//            }
//            List<TableFieldInfo> fieldList = table.getFieldList();
//            for (TableFieldInfo fieldInfo : fieldList) {
//                where.append(convertIfTag(fieldInfo, "", false));
//                where.append(" AND ").append(this.sqlCondition(fieldInfo.getCondition(),
//                        fieldInfo.getColumn(), "" + fieldInfo.getEl()));
//                where.append(convertIfTag(fieldInfo, true));
//            }
//            where.append("\n</where>");
//            return where.toString();
//        }
//    }
}

